﻿using System.Text.Json;
using Domain.Repository;
using Domain.Repository.RequestModels;
using Domain.Repository.ResponseModels;
using Infrastructure.RestSharpWrap;

namespace Repository.Implements
{
    public class TaskRepository : BaseRepository, ITaskRepository
    {
        public TaskRepository(IRestClientExt restClient) : base(restClient)
        {
        }

        public async Task<MobileSingleLimitResponseModel> MobileSingleLimitresult(MobileSingleLimitRequestModel model)
        {
            return await Request<MobileSingleLimitResponseModel, MobileSingleLimitRequestModel>("MobileSingleLimitresult", model);
        }

        public async Task<MobileSingleTaskNodeTreeResponseModel> MobileSingleTaskNodeTree(BaseRequestModel model)
        {
            return await Request<MobileSingleTaskNodeTreeResponseModel, BaseRequestModel>("MobileSingleTaskNodeTree", model);
        }

        public async Task<MobileSingleTraceLogResponseModel> MobileSingleTraceLog(BaseRequestModel model)
        {
            return await Request<MobileSingleTraceLogResponseModel, BaseRequestModel>("MobileSingleTraceLog", model);
        }

        public async Task<Dictionary<string, string>> MobileSingleTradeDetailNew(BaseRequestModel model)
        {
            var response = await Request<MobileSingleTradeDetailNewResponseModel, BaseRequestModel>(
                "MobileSingleTradeDetailNew",
                model, r => JsonSerializer.Deserialize<MobileSingleTradeDetailNewResponseModel>(JsonSerializer.Serialize(r)));

            if (response.rt.RE.IsSuccessful)
                return response.rt.TLlist.First().R.ToDictionary(key => key.NAME, val => val.txt);
            else
                return new Dictionary<string, string>(0);
        }
        public async Task<Dictionary<string, string>> MobileSingleTradeTarget(BaseRequestModel model)
        {
            var response = await Request<MobileSingleTradeTargetResponseModel, BaseRequestModel>(
                "MobileSingleTradeTarget",
                model, r => JsonSerializer.Deserialize<MobileSingleTradeTargetResponseModel>(JsonSerializer.Serialize(r)));

            if (response.rt.RE.IsSuccessful && (response.rt.TLlist?.Any() == true))
                return response.rt.TLlist.First().TARGETINDEX.ToDictionary(key => key.NAME, val => val.txt);
            else
                return new Dictionary<string, string>(0);
        }

        public async Task<CheckBatchResponseModel> MobileCheckBeforeActionByNodeId_Batch(MobileCheckBeforeActionByNodeIdBatchRequestModel model)
        {
            return await Request<CheckBatchResponseModel, BaseRequestModel>("MobileCheckBeforeActionByNodeId_Batch", model);
        }

        public async Task MobileApproveTaskByNodeId_Batch(MobileApproveTaskByNodeId_BatchRequestModel model)
        {
            await Request<dynamic, MobileApproveTaskByNodeId_BatchRequestModel>("MobileApproveTaskByNodeId_Batch", model);
        }
    }
}
